import Vuex from 'vuex'
import Vue from 'vue'
import App from './App.vue'
import { routes } from './routes/routes'
import VueRouter from 'vue-router'
import store from './store'
import VueBus from 'vue-bus'
import { Toast,MessageBox,InfiniteScroll,Spinner,Indicator,Popup,Loadmore,Field  } from 'mint-ui'
import VueResource from 'vue-resource'
import VueCookie from 'vue-cookie'
import { CONFIG } from './assets/js/config/index.js' // 获取配置信息
import VueTouch from 'js/touchEvent'


Vue.config.debug = true;
Vue.use(Vuex)
Vue.use(CONFIG); // 注册全局配置信息
Vue.use(VueRouter);
Vue.use(VueBus);
Vue.prototype.$toast=Toast
Vue.prototype.$messagebox=MessageBox
Vue.prototype.$indicator=Indicator
Vue.use(InfiniteScroll)
Vue.component(Spinner.name, Spinner);
Vue.component(Popup.name, Popup);
Vue.component(Loadmore.name, Loadmore);
Vue.component(Field.name, Field);
Vue.use(VueResource);
Vue.use(VueCookie);
Vue.use(VueTouch)



// 全局自定义指令自动聚焦元素by Lee
Vue.directive('focus', {
  // 当绑定元素插入到 DOM 中。
  update: function (el, { value }) {
    // 聚焦元素
    if (value) {
      el.focus()
    };
  }
})

//拦截器，有ajax请求时候，调用loading
Vue.http.interceptors.push(function (request, next) {
    this.$bus.emit('toggleLoading', true)
    next(function (response) {
        this.$bus.emit('toggleLoading', false);
    })
})
const router = new VueRouter({
    routes,
    mode: 'history',
    scrollBehavior(to, from, savedPosition) {

    }
});
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.isPublish)) {
        let vm = new Vue();
        if (from.name) {
            var url = window.location.protocol + "//" + window.location.host, shenceLogin;
            vm.$getCookie('phoneNumberOk') ? shenceLogin = true : shenceLogin = false;
        }
        if (to.matched.some(record => record.meta.requireAuth)) {
            vm.$getAccount().then(account => {
                if (account) {
                    vm.$setCookie("pcUserId", account.pcUserId);
                    vm.$CONSTANTS.isLogin = true;
                    vm.$setAccountInfo(account);
                    next();
                  } else {
                    var url = window.location.protocol + "//" + window.location.host;
                    if (to.matched.some(record => record.meta.toFullPath)) {
                        location.href = url + "/vue/login?returnUrl=" + encodeURIComponent(url + to.fullPath);
                    } else {
                        if (to.name == 'goodsPublish') {
                            location.href = url + "/vue/login?returnUrl=" + encodeURIComponent(url + to.fullPath);
                        } else {
                            location.href = url + "/vue/login?returnUrl=" + encodeURIComponent(location.href);
                        }
                    }
                }
            })
        }
        else {
            next();
        }
    } else {
        // 开发环境用next，线上用跳转404
        location.href = "/404.html";
        //next();
    }
});


var root = new Vue({
    store,
    router,
    render: h => h(App)
})

document.addEventListener('DOMContentLoaded', function () {
    root.$mount('#app')
})

